import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
// 引入svg需要用到插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// mock插件提供方法
import { viteMockServe } from 'vite-plugin-mock'
// 修改组件名字配置
import setupName from 'vite-plugin-vue-setup-extend'
export default defineConfig(({ command, mode }) => {
  // 获取各种环境下的对应的变量
  const env = loadEnv(mode, process.cwd());
  return {
    plugins: [
      vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
      viteMockServe({
        localEnabled: command === 'serve' // 保证开发阶段，可以使用mock接口
      }),
      setupName()
      
    ],
    resolve: {
      alias: {
        '@': path.resolve("./src")
      }
    },
    // 配置全局变量
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
            @use "@/styles/var.scss" as *;
          `
        }
      }
    },
    // 代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE, // 获取数据的服务器地址设置
          changeOrigin: true, // 是否需要代理跨域
          rewrite: (path) => path.replace(/^\/api/, ''), // 路径重写
        },
      }
    },

  }
})
